package cn.ac.big.bigd.bigsearch.service;
import cn.ac.big.bigd.bigsearch.ESFactory;
import cn.ac.big.bigd.bigsearch.Model.Entry;
import cn.ac.big.bigd.dao.NewAssemblyDao;
import cn.ac.big.bigd.dao.SubmissionDao;
import cn.ac.big.bigd.model.User;
import cn.ac.big.bigd.model.generate.NewAssembly;
import cn.ac.big.bigd.model.submit.Submission;
import cn.ac.big.bigd.util.DateUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.index.IndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *@Author wangfan
 *@Date 2016/11/29 15:53
 */

@Service
public class BigSearchService {
    @Autowired
    NewAssemblyDao newAssemblyDao;




    public  boolean createGwhAccession(String accession){
        String date = DateUtils.formatTime(new Date());
        NewAssembly newAssembly = newAssemblyDao.findByAccession(accession);
        if(newAssembly==null){
            return false;
        }
        Entry entry = new Entry();
        entry.setId(accession);
        entry.setType("Assembly");
        entry.setTitle(newAssembly.getName());
        entry.setUrl("http://bigd.big.ac.cn/gwh/Assembly/" + newAssembly.getId() + "/show");
        entry.setDbId("gwh");
        entry.setUpdatedAt(date);
        entry.setDescription("-");
        entry.setBasicInfo(newAssembly.getDescription() == null ? "-" : newAssembly.getDescription());
        List species = new ArrayList();
        species.add("plant");
        entry.setSpecies(species);
        Map attrs = new HashMap();
        attrs.put("Organism", newAssembly.getNewGenome().getScientificName());
        attrs.put("Common Names", newAssembly.getNewGenome().getCommonName());
        attrs.put("BioProject", "<a href='http://bigd.big.ac.cn/bioproject/browse/"+newAssembly.getBioProjectAccession()+"'>"+newAssembly.getBioProjectAccession()+ "</a>");
        attrs.put("BioSample", "<a href='http://bigd.big.ac.cn/biosample/browse/"+newAssembly.getBioSampleAccession()+"'>"+newAssembly.getBioSampleAccession()+ "</a>");
        attrs.put("Accession No.", "<a href='http://bigd.big.ac.cn/gwh/Assembly/"+newAssembly.getId()+"/show'>"+newAssembly.getAccession()+ "</a>");
        attrs.put("Download",(newAssembly.getDNA()==null?"DNA| ":"<a   download='"+newAssembly.getDNA()+" href='ftp://download.big.ac.cn/gwh/"+newAssembly.getFtpDir()+"/"+newAssembly.getDNA()+"'>DNA</a>| ") +
                ( newAssembly.getGFF()==null?"GFF| ":"<a   download='"+newAssembly.getGFF()+" href='ftp://download.big.ac.cn/gwh/"+newAssembly.getFtpDir()+"/"+newAssembly.getGFF()+"'>GFF</a>| " )+
                (newAssembly.getRNA()==null?"RNA| ":"<a   download='"+newAssembly.getRNA()+" href='ftp://download.big.ac.cn/gwh/"+newAssembly.getFtpDir()+"/"+newAssembly.getRNA()+"'>RNA</a>| " )+
                (newAssembly.getProtein()==null?"Protein| ":"<a   download='"+newAssembly.getProtein()+" href='ftp://download.big.ac.cn/gwh/"+newAssembly.getFtpDir()+"/"+newAssembly.getProtein()+"'>Protein</a> ") );
        if (newAssembly.getGenomeRepresentation().equals("0")) {
            attrs.put("Genome representation", "Full Genome");
        } else if (newAssembly.getGenomeRepresentation().equals("1")) {
            attrs.put("Genome representation", "Partial Genome");
        }
        if (0 == newAssembly.getAssemblyLevel()) {
            attrs.put("Assembly level", "Draft genome in contig level");
        } else if (1 == newAssembly.getAssemblyLevel()) {
            attrs.put("Assembly level", "Draft genome in scaffold level");
        } else if (2 == newAssembly.getAssemblyLevel()) {
            attrs.put("Assembly level", "Draft genome in chromsome level");
        } else if (3 == newAssembly.getAssemblyLevel()) {
            attrs.put("Assembly level", "Completed genome");
        }
        attrs.put("Release date", DateUtils.format(newAssembly.getReleasedAt()));
        entry.setAttrs(attrs);


        ObjectMapper mapper = new ObjectMapper();
        IndexResponse indexResponse = null;
        try {
            indexResponse = ESFactory.getClient().prepareIndex(ESFactory.INDEX, "gwh_entry",accession).setSource(mapper.writeValueAsString(entry)).execute().actionGet();
            if(indexResponse.getId().equals(accession)){
                return  true;
            }else{
                return false;
            }
        } catch (JsonProcessingException e) {
            return false;
        }


    }










}
